﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KhaoSatSinhVienTotNghiep.Model;
using KhaoSatSinhVienTotNghiep.Web.ViewModel;

namespace KhaoSatSinhVienTotNghiep.Web.Controllers
{
    public class DSKhaoSatController : Controller
    {
        //
        // GET: /DSKhaoSat/

        public ActionResult Index()
        {
            return View();
        }
        [HttpGet]
        public ActionResult AddNew()
        {
            return View();
        }

        [HttpPost]
        public ActionResult AddNew(FormCollection frm)
        {
            
             string ngaysinh=frm["ngaysinh"];
            string [] str=ngaysinh.Split('/');
            string pw="";

            string dscautl = frm["dscautl"].Trim();
            string[] ds = dscautl.Split(' ');

            for (int i = 0; i < str.Count(); i++)
            {
                pw += str[i];
            }
            var sv = new SinhVien
            {
                MSSV = frm["mssv"],
                HoTen = frm["hoten"],
                NgaySinh = DateTime.Parse(ngaysinh),
                GioiTinh = frm["sex"],
                DanToc = frm["dantoc"],
                DiaChi1 = frm["diachi"],
                DiaChi2 = "",
                DienThoai = frm["dienthoai"],
                Email = frm["email"],
                Lop = int.Parse(frm["MaLop"]),
                Password = pw
            };
            if (SinhVien.AddNew_Update(sv))
            {
                var dsks = new DSKhaoSat
                {
                    MaDotKS = int.Parse(frm["madotks"]),
                    MSSV = frm["MSSV"]

                };
                if (DSKhaoSat.AddNew(dsks))
                {
                    int madotks = int.Parse(frm["madotks"]);
                    string mssv = frm["MSSV"];
                    int madsks=DSKhaoSat.GetDSKhaoSatByMSSVAndMaDotKS(mssv, madotks);
                    for (int i = 0; i < ds.Count(); i++)
                    {
                        bool? status=CauTL.GetByID(int.Parse(ds[i])).Status ==null ? false : CauTL.GetByID(int.Parse(ds[i])).Status ;
                        if (status == false)
                        {
                            var dstl = new DSTL
                            {
                                MaCauTL = int.Parse(ds[i]),
                                MaDSKhaoSat = madsks,
                                Khac = ""
                            };
                            DanhSachTraLoi.AddNew(dstl);
                        }
                        else
                        {
                            var dstl = new DSTL
                            {
                                MaCauTL = int.Parse(ds[i]),
                                MaDSKhaoSat = madsks,
                                Khac = frm[ds[i]]
                            };
                            DanhSachTraLoi.AddNew(dstl);
                        }
                    }
                }
            }
            return RedirectToAction("Index");
        }
    }
}
